#include "CommonShare.h"


shm_struct * m_pshmArray;
int m_shmArraySize;
int shmid_IntData;
int shmid_CharData;
int shmid_ArmData;

int * pshmIntData;
char * pshmCharData;
ArmSysInfo *pshmArmData;



void init_share()
{
    // 不能用非常量来定义全局变量，所以放里面
    shm_struct shmArray[]={
        {INT_DATA_KEY,shmid_IntData,sizeof(int)*10,(void **)&pshmIntData},
        {CHAR_DATA_KEY,shmid_CharData,sizeof(char)*10,(void **)&pshmCharData},
        {ARM_DATA_KEY,shmid_ArmData,sizeof(ArmSysInfo),(void **)&pshmArmData},
    };
    /*
    int shmget(key_t key, size_t size, int shmflg);   
            key： 对应IPC对象的key值
	        size： 共享内存的字节数  【PAGE_SIZE的倍数】  PAGE_SIZE=1024
	        shmflg：
                IPC_CREAT|0666  -> 不存在则创建
		        IPC_EXCL   -> 存在则报错

    void *shmat(int shmid, const void *shmaddr, int shmflg);
    	    shmid：共享内存ID号
	        shmaddr： 共享内存分配起始地址
		        NULL  -> 【系统自动分配】
		        非NULL -> 用户自定义起始地址
	        shmflg：普通正常属性 -> 0
    */

    m_pshmArray = (shm_struct *)malloc(sizeof(shmArray));
    if(m_pshmArray == NULL)
    {
        printf("malloc error\n");
        exit(-1);
    }
    memcpy(m_pshmArray,shmArray,sizeof(shmArray));

    m_shmArraySize = sizeof(shmArray) / sizeof(shmArray[0]);
    printf("m_shmArraySize = %d , sizeof(shmArray) = %d,sizeof(shmArray[0]) = %d\n",m_shmArraySize,sizeof(shmArray),sizeof(shmArray[0]));

    for(int i=0;i<m_shmArraySize;i++)
    {
        m_pshmArray[i].shmid = shmget(m_pshmArray[i].key,m_pshmArray[i].size,IPC_CREAT|0666);
        *(m_pshmArray[i].addr) = shmat(m_pshmArray[i].shmid,NULL,0);
    }
}


void writeShmArray(int scale)
{
    //int * t_pshmInt = (int *)pshmIntData;
    //char * t_pshmChar = (char *)pshmCharData;
    
    printf("writeShmArray()\n");
    // 应该借用锁或者信号量，做互斥
    for(int i=0;i<10;i++)
    {
        ((int *)pshmIntData)[i] = i*scale;
        //t_pshmInt[i] = i*5;
        *(pshmCharData + i) = 'a' + 2*scale;
    }

}

void readShmArray()
{
    printf("readShmArray()\n");
    for(int i=0;i<10;i++)
    {
        printf("pshmIntData[%d] = %d,\t pshmCharData[%d] = %d\n",i,pshmIntData[i],i,pshmCharData[i]);
    }
}


void dest_share()
{
    for(int i=0;i<m_shmArraySize;i++)
    {
        // 撤销内存空间
        shmdt(m_pshmArray[i].addr);
        // 删除IPC对象
        shmctl(m_pshmArray[i].shmid,IPC_RMID,NULL);
    }
}


ArmSysInfo* get_pShmArmSysInfo()
{
    return pshmArmData;
}



int insQueue(queueStruct *arg,int value)
{
    if(arg->count == arg->maxSize)
        return -1;
    arg->array[arg->rear] = value;
    arg->count++;
    arg->rear = (arg->rear+1) % arg->maxSize;
}

int delQueue(queueStruct *arg,int *value)
{
    if(arg->count==0)
        return -1;
    *value = arg->array[arg->front];
    arg->count--;
    arg->front = (arg->front+1) % arg->maxSize;
}

int initQueue(queueStruct *arg)
{
    arg->maxSize = sizeof(arg->array)/sizeof(arg->array[0]);
    if(arg->maxSize<1);
        return -1;
    arg->count = 0;
    arg->front = 0;
    arg->rear = arg->front+1;
}

int destroyQueue(queueStruct *arg)
{
    arg->rear = arg->front = 0;
    arg->count = 0;
}

